package studentManage;

import java.util.ArrayList;
import java.util.Scanner;

public class Test {

	static Scanner sc = new Scanner(System.in);

	StudentModel studentModel = new StudentModel();
	MajorModel majorModel = new MajorModel();

	// 将学生/专业信息存放在ArrayList集合中
	static ArrayList<StudentModel> students = new ArrayList<StudentModel>();
	static ArrayList<MajorModel> majorModels = new ArrayList<MajorModel>();

	// 用户角色选择
	void identfy() {
		while (true) {
			System.out.println("*****************");
			System.out.println("请选择：");
			System.out.println("1-学生");
			System.out.println("2-管理员");
			System.out.println("3-退出系统");
			System.out.println("*****************");
			String n = sc.nextLine();
			int k = Integer.parseInt(n);
			switch (k) {
			case 1:
				stuLogin();
				break;
			case 2:
				admin();
				break;
			case 3:
				System.out.println("goodbye!");
				return;
			default:
				break;
			}

		}
	}

	// 管理员
	public void admin() {
		while (true) {
			System.out.println("*****************");
			System.out.println("请选择：");
			System.out.println("1-学生管理");
			System.out.println("2-专业管理");
			System.out.println("3-退出登录");
			System.out.println("*****************");
			String n = sc.nextLine();
			switch (Integer.parseInt(n)) {
			case 1:
				studentManage();
				break;
			case 2:
				majorManage();
				break;
			case 3:
				return;
			default:
				break;
			}
		}
	}

	// 学生管理
	public void studentManage() {
		while (true) {

			System.out.println("*****************");
			System.out.println("请选择：");
			System.out.println("1-显示全部学生信息");
			System.out.println("2-添加学生信息");
			System.out.println("3-修改学生信息");
			System.out.println("4-删除学生信息");
			System.out.println("5-根据学号查询学生信息");
			System.out.println("6-根据姓名查询学生信息");
			System.out.println("7-返回");
			System.out.println("*****************");
			String n = sc.nextLine();
			switch (Integer.parseInt(n)) {
			case 1:
				studentModel.print(students, majorModels);
				break;
			case 2:
				studentModel.addStudent(students, majorModels);
				break;
			case 3:
				System.out.println("****修改学生信息****");
				System.out.println("请输入您要修改的学生的学号：");
				String uid = sc.nextLine();
				int ui = studentModel.searchStudent(students, Integer.parseInt(uid));
				studentModel.updataStudent(students, majorModels, ui);
				break;
			case 4:
				System.out.println("****删除学生信息****");
				System.out.println("请输入您要删除的学生学号：");
				String did = sc.nextLine();
				int di = studentModel.searchStudent(students, Integer.parseInt(did));
				studentModel.delStudent(students, di);
				break;
			case 5:
				System.out.println("****查询学生信息****");
				System.out.println("请输入您要查询的学生学号：");
				String sid = sc.nextLine();
				int si = studentModel.searchStudent(students, Integer.parseInt(sid));
				studentModel.message(students, majorModels, si);
				break;
			case 6:
				System.out.println("****查询学生信息****");
				System.out.println("请输入您要查询的学生姓名：");
				String name = sc.nextLine();
				int ni = studentModel.searchName(students, name);
				studentModel.message(students, majorModels, ni);
				break;
			case 7:
				return;
			default:
				break;
			}
		}
	}

	// 专业管理
	public void majorManage() {
		MajorModel majorModel = new MajorModel();
		while (true) {
			System.out.println("请选择：");
			System.out.println("1-增加专业");
			System.out.println("2-删除专业");
			System.out.println("3-修改专业");
			System.out.println("4-根据id查找专业");
			System.out.println("5-根据专业名查找专业");
			System.out.println("6-显示全部专业");
			System.out.println("7-退出");
			int i = Integer.parseInt(sc.nextLine());
			if (i == 7) {
				return;
			}
			switch (i) {
			case 1:
				majorModel.addMajor(majorModels);
				break;
			case 2:
				System.out.println("请输入您要删除的专业id");
				int did = Integer.parseInt(sc.nextLine());
				majorModel.del(majorModels, majorModel.searchById(majorModels, did));
				break;
			case 3:
				System.out.println("请输入您要修改的专业id");
				int uid = Integer.parseInt(sc.nextLine());
				majorModel.updateMar(majorModels, majorModel.searchById(majorModels, uid));
				break;
			case 4:
				System.out.println("请输入您要查找的专业id");
				int sid = Integer.parseInt(sc.nextLine());
				majorModel.majorMes(students, majorModels, majorModel.searchById(majorModels, sid));
				break;
			case 5:
				System.out.println("请输入您要查找的专业名");
				String name = sc.nextLine();
				majorModel.majorMes(students, majorModels, majorModel.searchByName(majorModels, name));
				break;
			case 6:
				majorModel.searchAll(students, majorModels);
			default:
				break;
			}

		}
	}

	// 学生
	// 登录
	public void stuLogin() {
		System.out.println("请输入您的学号：");
		String id = sc.nextLine();
		System.out.println("请输入密码：");
		String password = sc.nextLine();
		int i = studentModel.searchStudent(students, Integer.parseInt(id));// i是学生集合的下标
		if (i == -1) {
			System.out.println("不存在该账号，请注册！");
			studentModel.addStudent(students, majorModels);
		} else {
			if (password.equals(students.get(i).getPassword())) {
				System.out.println("*****登录成功*****");
				student(i);
			} else {
				System.out.println("密码不正确，请重新登录！");
				System.out.println("*****************");

			}
		}
	}

	// 学生
	public void student(int i) {
		while (true) {
			System.out.println("*****************");
			System.out.println("请选择：");
			System.out.println("1-显示学生信息");
			System.out.println("2-修改个人信息");
			System.out.println("3-退出登录");
			System.out.println("*****************");
			String n = sc.nextLine();
			switch (Integer.parseInt(n)) {
			case 1:
				studentModel.message(students, majorModels, i);
				break;
			case 2:
				studentModel.updataStudent(students, majorModels, i);
				break;
			case 3:
				return;
			default:
				break;
			}
		}

	}

	public static void main(String[] args) {

		Test test = new Test();
		test.identfy();
	}

}
